package edu.yzu.testspringboot002.convert;

import cn.hutool.core.collection.CollUtil;

import edu.yzu.testspringboot002.common.constant.CacheColumnFamilyConstants;
import edu.yzu.testspringboot002.po.Dept;
import edu.yzu.testspringboot002.po.LogLogin;
import edu.yzu.testspringboot002.po.User;
import edu.yzu.testspringboot002.po.dto.SysLogLoginDTO;
import edu.yzu.testspringboot002.po.dto.SysUserDTO;
import edu.yzu.testspringboot002.utils.RocksDBUtil;
import org.mapstruct.Mapper;
import org.mapstruct.NullValuePropertyMappingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单转换映射器
 *
 * @author yulang
 * @date 2023-08-18
 */

@Mapper(
        componentModel = "spring",
        nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE
)
public abstract class UserConvertMapper {

    public abstract SysUserDTO userToSysUserDTO(User user);

    public List<SysUserDTO> toSysUserDTOList(List<User> userList) {
        if (CollUtil.isEmpty(userList)) {
            return new ArrayList<>();
        }

        List<SysUserDTO> list = new ArrayList<SysUserDTO>(userList.size());
        for (User user : userList) {
            SysUserDTO sysUserDTO = userToSysUserDTO(user);
            Dept dept = RocksDBUtil.get(CacheColumnFamilyConstants.DEPT_INFO, sysUserDTO.getDeptId().toString(), Dept.class);
            if (dept != null) {
                sysUserDTO.setDeptName(dept.getName());
            }
            list.add(sysUserDTO);
        }

        return list;
    }

    public abstract List<SysLogLoginDTO> toLoginLogListDTO(List<LogLogin> logLoginList);

    public abstract User toSysUserPO(SysUserDTO sysUserDTO);
}

